package timing.ukulele.flow.controller;

import timing.ukulele.flow.util.ProcessImageManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

import static org.springframework.web.context.request.RequestContextHolder.getRequestAttributes;
@RestController
@RequestMapping("Image")
@Api(tags = "流程图接口")
@Slf4j
public class ImageController {

    @Autowired
    private ProcessImageManager processImageManager;

    @GetMapping("/getByProcessInstanceId/{processInstanceId}")
    @ApiOperation("根据流程实例id查看实时流程图")
    public void getImageByProcessInstanceId(
            @ApiParam(value = "流程实例id", required = true) @PathVariable("processInstanceId") String processInstanceId,
            @ApiParam("租户id") @RequestParam("tenantId") String tenantId) {
        HttpServletResponse response = ((ServletRequestAttributes) getRequestAttributes()).getResponse();
        try {
            InputStream is = processImageManager.getFlowImgByProcInstId(processInstanceId, tenantId);
            if (is == null) {
                return;
            }
            byte[] b = new byte[1024];
            int len = -1;
            while ((len = is.read(b, 0, 1024)) != -1) {
                response.getOutputStream().write(b, 0, len);
            }
            is.close();
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
}

